#! /bin/bash
# 注：使用本脚本需要确保/usr/local/src/下有对应版本的nginx包（默认1.19.0）

# 部署epel源
if [ ! -f "/etc/yum.repos.d/epel.repo" ];then
	wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
fi

# 准备环境、安装依赖
cd /usr/local/src/
tar -zxf nginx-1.19.0.tar.gz
cd nginx-1.19.0/
useradd nginx -s /sbin/nologin -M
yum -y install gcc gcc-c++ make pcre-devel openssl-devel

# 编译、安装nginx
./configure \
--prefix=/usr/local/nginx \
--user=nginx \
--group=nginx \
--with-stream \
--with-http_stub_status_module \
--with-http_ssl_module
make && make install

# 启动nginx、设置开机自启
cat > /lib/systemd/system/nginx.service <<-EOF
[Unit]	
Description=nginx service							
After=network.target								

[Service]
Type=forking										
ExecStart=/usr/local/nginx/sbin/nginx				
ExecReload=/usr/local/nginx/sbin/nginx -s reload	
ExecStop=/usr/local/nginx/sbin/nginx -s stop		
PrivateTmp=true									

[Install]
WantedBy=multi-user.target
EOF

systemctl start nginx
systemctl enable nginx

# 安装、启动php-fpm
yum -y install php php-fpm php-mysql mariadb-devel
systemctl start php-fpm
systemctl enable php-fpm

# 安装、启动mariadb
yum -y install mariadb mariadb-server mariadb-devel
systemctl start mariadb
systemctl enable mariadb

# 配置动静分离
cd /usr/local/nginx/conf/
sed -i '45i\            index index.php index.html index.htm;' nginx.conf
sed -i '46d' nginx.conf
sed -i '65,71s/#//' nginx.conf
sed -i '69s/^/#/' nginx.conf
sed -i '70s/_params/.conf/' nginx.conf
systemctl restart nginx